package com.wjd.engine;

import com.wjd.parser.ChineseParser;
import com.wjd.store.QueryResult;
import com.wjd.store.Sample;
import com.wjd.store.Store;

import java.util.*;

public class SearchEngine {

    // 总共的网页数量
    static int totalPage = 2000;

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String search = s.next();
        SearchEngine searchEngine = new SearchEngine();
        SearchResult[] queue = searchEngine.search(search);
        int length = queue.length;
        System.out.println(length);
        for (SearchResult searchResult : queue) {
            System.out.println(searchResult.getURL() + "\n" + searchResult.getTitle() + "\n" + searchResult.getContent() + "\n");
        }
    }

    // 搜索信息
    public SearchResult[] search(String keywords) {
        // 先对查询语句进行中文分词
        ChineseParser parser = new ChineseParser();
        Map<String, Double> tf = parser.getTF(keywords);
        // 返回和关键字有关的所有网页
        Set<String> searchKeySet = tf.keySet();
        QueryResult[] results = Store.getInstance().query(searchKeySet);
        if (results.length == 0) {
            return new SearchResult[0];
        }
        // 根据关键字查找网页并将其排序
        return sortResults(results, tf);
    }

    /**
     * 将网页按其重要性排好序，以队列形式返回
     */
    private SearchResult[] sortResults(QueryResult[] results, Map<String, Double> tf) {
        HashMap<String, SearchResult> pageValues = new HashMap<>();
        for (QueryResult result : results) {
            if (result.res == null) {
                continue;
            }

            // 逆文本频率指数=log(总网页数/有该关键字出现的网页数量)，用于计算网页排名
            double idf = Math.log((double) totalPage / result.res.size());

            String word = result.getWord();
            double freq = tf.get(word);
            for (Sample sample : result.res) {
                // 关键字在网页中的词频
                Double wordTF = sample.getWordTF();
                // 网页的价值，目前只有出度，出度越大，价值越低
                double pageValue = sample.getPageValue();
                pageValue = pageValue != 0 ? 1 / pageValue : 1;

                // 网页的综合排名数值=网页重要性*网页相关性（即关键字词频*逆文本频率指数）*词频
                double pageRank = pageValue * wordTF * idf * freq;

                // 添加/更新网页排名值
                String url = sample.getUrl();
                SearchResult ret = pageValues.get(url);
                if (ret == null) {
                    ret = new SearchResult();
                    ret.setURL(url);
                    ret.setTitle(sample.getTitle());
                    ret.setContent(sample.getContent());
                    pageValues.put(url, ret);
                }
                pageRank += ret.getPageValue();
                ret.setPageValue(pageRank);
            }
        }

        // 进行排序
        List<SearchResult> l = new ArrayList<>(pageValues.values());
        l.sort((o1, o2) -> {
            double result = o2.getPageValue() - o1.getPageValue();
            if (result > 0)
                return 1;
            else if (result == 0)
                return 0;
            else
                return -1;
        });
        return l.toArray(new SearchResult[0]);
    }

}
